//
//  CJExten+UIButton.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-18.
//

import UIKit
import Foundation

public extension UIButton {
    
    private struct CJButtonKey {
        static var cj_buttonClosure: Void?
    }
    
    /// 添加点击回调
    @objc func cj_addAction(closure: @escaping (UIButton) -> (), for controlEvents: UIControl.Event = .touchUpInside) {
        objc_setAssociatedObject(self, &CJButtonKey.cj_buttonClosure, closure, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        addTarget(self, action: #selector(cj_buttonClosureAction), for: .touchUpInside)
    }
    
    /// button点击事件
    @objc private func cj_buttonClosureAction() {
        if let closure = objc_getAssociatedObject(self, &CJButtonKey.cj_buttonClosure) as? ((UIButton) -> ()) {
            closure(self)
        }
    }
    
}
